<?php

include "kernel.php";
include "adw_functions.php";
include "config2.php";

		$SUM = 0;

		$id = $_SERVER["argv"][1];

		$pilot_r = mysql_query("select UNIX_TIMESTAMP(starttime) as s,
        							   UNIX_TIMESTAMP(endtime) as e,
                                       pilotid,

                                       (eSAir+eTank+eCar+eArt+eAaa+eWag+eShip) as ground,

						               eAir-eBom as air,
                                       eBom as bom,

                                       eSAir as sair,
                                       eTank as tank,
                                       eCar as car,
                                       eArt as art,
                                       eAaa as aaa,
                                       eWag as wag,

										addfuel,
                                        addammo,
                                        addrepair,
                                        dfuel,
                                        dammo,
                                        drepair,


									   SortieEND as end,
                                       Wound as wound,
						               UpdateSorties as upd

                   from sorties where id='$id'");

		$sortie = mysql_fetch_assoc($pilot_r);

        $pilot = $sortie[pilotid];

        $fpilot_r = mysql_query("select * from finalpilots where pilotid='".$pilot."'");
        $squadronid = mysql_fetch_assoc($fpilot_r);
        $squadronid = $squadronid[squadronid];



        $s = $sortie[s];
        $e = $sortie[e];
        $end = $sortie[end];

        $upd = $sortie[upd];
        $tank = $sortie[tank];
        $sair = $sortie[sair];
        $art = $sortie[art];
        $aaa = $sortie[aaa];
        $car = $sortie[car];
        $wag = $sortie[wag];
        $ship = $sortie[ship];
        $air = $sortie[air]-$sortie[bom];
        $bom = $sortie[bom];
        $ground = $sortie[ground];
        $w = $sortie[wound];


	    if (($end != 'MIA' and $end != 'KICK' and $end != 'DIEDISCO') and ($e - $s >= 15*60 or $end != 'TKON' or $air>0 or $ground>0  or $sortie[addammo]>0 or $sortie[addfuel]>0 or $sortie[addrepair]>0 or $sortie[dfuel]>0 or $sortie[dammo]>0 or $sortie[drepair]>0))
        	$fightflight = 1;
        else
        	$fightflight = 0;


	            $sifd = $sortie[end];
	            if ($sifd == 'DIED')
	                $SUM -= 40;
	            elseif ($sifd=='CAPTURED')
	                $SUM -= 40;
	            elseif ($sifd=='DIEDISCO')
	                $SUM -= 40;
	            elseif ($sifd=='MIA')
	                $SUM -= 20;
	            elseif ($sifd=='BAIL')
	                $SUM -= 20;
	            elseif ($sifd=='CRASHED')
	                $SUM -= 10;

        if ($sifd=='TKON' and $w)
			$SUM += 7;

        if ($sifd=='TKON' and $fightflight)
			$SUM += 5;

        $SUM += ($air*30 + $bom*60 + $tank*7 + $art*3 + $aaa*5 + $sair*10 + $wag*1 + $car*1 + $ship*100);



	        $link = ConnectToBase ();
            if (!$upd) {
            	echo "SUM:$SUM\r\n";
            	mysql_query("update sorties set UpdateSorties='1' where id='$id'");
	            UpdateSquadSums($SUM,$pilot);
                UpdateSquadronCommonStat($id,$squadronid);
                mysql_query("update squadrons set uc=uc+1 where id='$squadronid'");
            }

		    UpdatePilotCommonStat ($pilot);

            $b_r = mysql_query("select battlepoints from squadrons where id='$squadronid'");
            $b = mysql_fetch_assoc($b_r);
			$logf = fopen("battlepoints.log","a");
            fwrite($logf,"Parsing sortie $id (squad:$squadronid, pilot:$pilot). Is a battlesortie = ");

            if ($fightflight and !$upd) {
            	mysql_query("update squadrons set sorties=sorties+1 where id='$squadronid'");
            	mysql_query("update squadrons set allsorties=allsorties+1 where id='$squadronid'");

                fwrite($logf,"1");
            	mysql_query("update squadrons set battlepoints=battlepoints+1 where id='$squadronid'");

            }
            elseif (!$fightflight and !$upd) {
            	mysql_query("update squadrons set allsorties=allsorties+1 where id='$squadronid'");
                fwrite($logf,"0");
            }
            else {
                fwrite($logf,"Does not matter, was already parsed");
            }

            fwrite($logf,". Battlepoints was: $b[battlepoints]. ");

            $b_r = mysql_query("select battlepoints from squadrons where id='$squadronid'");
            $b = mysql_fetch_assoc($b_r);

            fwrite($logf,"Battlepoints now: $b[battlepoints].\r\n");

            fclose($logf);

?>